﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using FinosLibrary;
using FinosServiceLibrary.Server;
namespace BattleShip {
    [MessageProcess]
    public class Login:ServerLogic {
        [AutoWire]
        public DataClassesDataContext database;
        [MessageMethod]
        public void UserLogin(string client,string[] info) {
            if (!database.DatabaseExists())
                database.CreateDatabase();
            var theUser = getUserByName(info[0]);
            var loginLog = new UserLoginLog();
            loginLog.time = DateTime.Now;
            loginLog.ip = getClientById(client).remoteEndPoint.ToString();
            var wrongUsers = database.Users.Where(user => user.connId == client);
            foreach (var wrongUser in wrongUsers) {
                wrongUser.connId = null;
            }
            if (theUser != null) {
                if (theUser.password == info[1]) {
                    theUser.lastlogin = loginLog.time;
                    theUser.connId = client;
                    call(client, "Login_Success", theUser);
                    loginLog.userId = theUser.id;
                    database.UserLoginLogs.InsertOnSubmit(loginLog);
                    database.SubmitChanges();
                } else {
                    database.SubmitChanges();
                    call(client, "Login_Faild", "密码错误");
                    getClientById(client).onDisconnect();
                }
            } else {
                var newUser = new User();
                newUser.name = info[0];
                newUser.password = info[1];
                newUser.ip = loginLog.ip;
                newUser.connId = client;
                newUser.lastlogin = loginLog.time;
                newUser.face = database.FaceLists.First().path;
                newUser.shell = new string[] { "Head1", "Body0", "Wing1", "Back1" }.ToJson();
                newUser.item = new int[] { 1, 2, 3, 4 }.ToJson();
                database.Users.InsertOnSubmit(newUser);
                database.SubmitChanges();
                newUser = getUserByName(newUser.name);
                loginLog.userId = newUser.id;
                database.UserLoginLogs.InsertOnSubmit(loginLog);
                database.SubmitChanges();
                call(client, "Regist_Success", newUser);
            }
        }
        [MessageMethod]
        public void onDisconnect(string client) {
            var theUser = getUserById(client);
            if (theUser != null) {
                theUser.connId = null;
                database.SubmitChanges();
            }
        }
        [MessageMethod]
        public void GetFaceList(string client) {
            call(client, "GotFaceList", database.FaceLists.ToArray());
        }
        [MessageMethod]
        public void ChangeFace(string client,FaceList theface) {
            var theUsers = getUserById(client);
            theUsers.face = theface.path;
            database.SubmitChanges();
            call(client, "ChangeFace_Success", theface.path);
        }
        [MessageMethod]
        public void ChangePassword(string client, string newPassword) {
            var theUsers = getUserById(client);
            theUsers.password = newPassword;
            database.SubmitChanges();
            call(client, "ChangePassword_Success");
        }
        private User getUserById(string client) {
            return database.Users.SingleOrDefault(user => user.connId == client);
        }
        private User getUserByName(string name) {
            return database.Users.SingleOrDefault(user => user.name == name);
        }
    }
}
